package com.tudouji.common.utils;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import com.tudouji.common.utils.sign.Md5Utils;
import org.apache.axis.encoding.Base64;


public class SecurityUtil {
	private static final String ALGORITHM = "AES";

	/**
	 * 生成密钥
	 * 
	 * @return
	 * @throws Exception
	 */
	// private static SecretKey geneKey() throws Exception {
	// // 获取一个密钥生成器实例
	// KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
	// SecureRandom random = new SecureRandom();
	// random.setSeed("Zaq1xsw2cde3$rfvXsw2!wdv!IIMGFDMB%%&21fsdftyujthdfsaE%Y&dqqq!!00OOQQ".getBytes());//
	// 设置加密用的种子，密钥
	// keyGenerator.init(random);
	// SecretKey secretKey = keyGenerator.generateKey();
	// // 把上面的密钥存起来
	// Path keyPath = Paths.get("D:/harvest_aes.key");
	// Files.write(keyPath, secretKey.getEncoded());
	// return secretKey;
	// }

	/**
	 * 读取存储的密钥
	 * 
	 * @param keyPath
	 * @return
	 * @throws Exception
	 */
	private static SecretKey readKey(Path keyPath) throws Exception {
		// 读取存起来的密钥
		byte[] keyBytes = Files.readAllBytes(keyPath);
		SecretKeySpec keySpec = new SecretKeySpec(keyBytes, ALGORITHM);
		return keySpec;
	}

	public static String encode(String str) throws Exception {
		// 1、指定算法、获取Cipher对象
		Cipher cipher = Cipher.getInstance(ALGORITHM);// 算法是AES
		// 2、生成/读取用于加解密的密钥
		Path keyPath = Paths.get("/newERP/jar/harvest_aes.key");
		SecretKey secretKey = readKey(keyPath);
		// 3、用指定的密钥初始化Cipher对象，指定是加密模式，还是解密模式
		cipher.init(Cipher.ENCRYPT_MODE, secretKey);
		// 4、更新需要加密的内容
		cipher.update(str.getBytes());
		// 5、进行最终的加解密操作
		byte[] result = cipher.doFinal();// 加密后的字节数组
		// 也可以把4、5步组合到一起，但是如果保留了4步，同时又是如下这样使用的话，加密的内容将是之前update传递的内容和doFinal传递的内容的和。
		// byte[] result = cipher.doFinal(content.getBytes());
		String base64Result = Base64.encode(result).toString();// 对加密后的字节数组进行Base64编码
		System.out.println("Result: " + base64Result);
		return base64Result;
	}

	public static String decode(String str) throws Exception {
		Cipher cipher = Cipher.getInstance(ALGORITHM);

		FileUtils fileUtils = new FileUtils();
		String path = fileUtils.getPath();
		String filePath = path + "templates/harvest_aes.key";
		//测试E:/workspace/service-tudouji/src/main/resources/harvest_aes.key
		Path keyPath = Paths.get("/newERP/jar/harvest_aes.key");
		SecretKey secretKey = readKey(keyPath);
		cipher.init(Cipher.DECRYPT_MODE, secretKey);
		byte[] encodedBytes = Base64.decode(str);
		byte[] result = cipher.doFinal(encodedBytes);// 对加密后的字节数组进行解密
		System.out.println("Result: " + new String(result));
		return new String(result);
	}

	public static void main(String[] args) throws Exception {
		String a = Md5Utils.hash("SF22031132.47531.938432.123320220325 11:20:35eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6ImE0NDZmZGEzLTEyNTktNDZhNi05NmMwLTMxMTFjOTJhNDM2ZSJ9.dRxA_b9_gs-J0qxTndTkn37UxQs6do1t6wdAGyyszorE1IsJeEORmsVtZ2qgNf2MP7-v7FqDzqErtXGq0UKoAQ");
		System.out.println(a);
	}
}
